﻿using System;

using PMngr.Data;

namespace PMngr.Main
{
    public partial class Single : BaseControl
    {
        private IRepositoryFactory repositoryFactory;

        public Single()
        {
            InitializeComponent();          
        }

        public Single(IRepositoryFactory repositoryFactory) : this()
        {
            this.repositoryFactory = repositoryFactory;          
        }

        public override void InitializeControls()
        {  
            EditCtl.Initialize(repositoryFactory); 
        }

        private void SaveToolStripMenuItemClick(object sender, EventArgs e)
        {
            using (var repository = repositoryFactory.CreateRepository())
            {
                var profile = EditCtl.GetProfile(repository);
                if (profile != null)
                {
                    repository.Add(profile);
                    repository.CommitChanges();

                    // 清除内容
                    EditCtl.Reset();

                    LogTrace(String.Format(Utilities.SuccessAdded, profile.Name));
                }
            }
        }
    }
}
